package com.ruoyi.common;

import com.alibaba.fastjson2.JSONObject;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service("kongAdmin")
public class KongAdmin {
    private String Url = "http://150.158.108.76:8001/";
    String response = null;
    private RestTemplate restTemplate = new RestTemplate();
    private Requestcommon requestcommon = new Requestcommon();

    public String getIt(String url){
        response=requestcommon.GetKong(Url+url);
        return response;
    }

    /**
     * 创建server
     * requestBody
     */
    public String CreateService(JSONObject requestBody) {
        response = requestcommon.PostKong(Url + "services", requestBody);
        return response;
    }


    /**
     * 获取所有服务
     *
     * @param size
     * @return
     */
    public String ListAllService(int size) {
        if (size > 0) {
            response = requestcommon.GetKong(Url + "services?size=" + size);
        } else {
            response = requestcommon.GetKong(Url + "services?size=100");
        }

        return response;
    }

    /**
     * 删除服务
     *
     * @param serviceName
     * @return
     */
    public String DeleteService(String serviceName) {
        response = requestcommon.DeleteKong(Url + "services/" + serviceName);
        return response;
    }

    /**
     * 更新服务
     *
     * @param serviceName
     * @param requestBody
     * @return
     */
    public String PutService(String serviceName, JSONObject requestBody) {
        response = requestcommon.PutKong(Url + "services/" + serviceName, requestBody);
        return response;
    }

    /**
     * 更新服务
     *
     * @param serviceName
     * @param requestBody
     * @return
     */

    public String PatchService(String serviceName, JSONObject requestBody) {
        response = requestcommon.PatchKong(Url + "services/" + serviceName, requestBody);
        return response;
    }

    /**
     * 获取服务
     *
     * @param serviceName
     * @return
     */
    public String FetachService(String serviceName) {
        response = requestcommon.GetKong(Url + "services/" + serviceName);
        return response;
    }

    /**
     * 创建route
     *
     * @param requestBody
     * @param serviceName
     * @return
     */
    public String CreateRoute(JSONObject requestBody, String serviceName) {
        response = requestcommon.PostKong(Url + "services/" + serviceName + "/routes", requestBody);
        return response;
    }

    /**
     * 获取所有route
     *
     * @param serviceName
     * @param size
     * @return
     */
    public String ListAllRouteInService(String serviceName, int size) {
        if (size > 0) {
            return response = requestcommon.GetKong(Url + "services/" + serviceName + "/routes?size=" + size);
        } else {
            return response = requestcommon.GetKong(Url + "services/" + serviceName + "/routes?size=100");
        }
    }

    public String ListAllRoute(int size) {
        if (size > 0) {
            return response = requestcommon.GetKong(Url + "routes?size=" + size);
        } else {
            return response = requestcommon.GetKong(Url + "routes?size=100");
        }
    }

    /**
     * 删除route
     *
     * @param serviceName
     * @param routeName
     * @return
     */
    public String DeleteRoute(String serviceName, String routeName) {
        response = requestcommon.DeleteKong(Url + "services/" + serviceName + "/routes/" + routeName);
        return response;
    }

    /**
     * 更新route
     *
     * @param serviceName
     * @param routeName
     * @param requestBody
     * @return
     */

    public String PutRoute(String serviceName, String routeName, JSONObject requestBody) {
        response = requestcommon.PutKong(Url + "services/" + serviceName + "/routes/" + routeName, requestBody);
        return response;
    }

    /**
     * 更新route
     *
     * @param serviceName
     * @param routeName
     * @param requestBody
     * @return
     */
    public String PatchRoute(String serviceName, String routeName, JSONObject requestBody) {
        return response = requestcommon.PatchKong(Url + "services/" + serviceName + "/routes/" + routeName, requestBody);
    }

    public String FetachRoute(String serviceName, String routeName) {
        return response = requestcommon.GetKong(Url + "services/" + serviceName + "/routes/" + routeName);


    }

    /**
     * 创建plugin
     *
     * @param requestBody
     * @param serviceName
     * @param PluginName
     * @return
     */
    public String CreatePluginInService(JSONObject requestBody, String serviceName, String PluginName) {
        response = requestcommon.PostKong(Url + "services/" + serviceName +  "/plugins", requestBody);
        return response;
    }

    public String CreatePluginInConsumer(JSONObject requestBody, String CName,String PluginName) {
        response = requestcommon.PostKong(Url + "consumers/" + CName +"/"+PluginName, requestBody);
        return response;
    }

    public String DeletePluginInConsumer(String cname, String pluginName,String group) {

        return response = requestcommon.DeleteKong(Url + "consumers/" + cname +"/"+pluginName+"/"+group);
    }
    public String DeletePluginByID( String pluginID) {
        return response = requestcommon.DeleteKong(Url + "plugins/" + pluginID);
    }
    /**
     * 获取所有plugin某个服务
     *
     * @param serviceName
     * @param size
     * @return
     */
    public String ListAllPluginInService(String serviceName, int size) {
        if (size > 0) {
            response = requestcommon.GetKong(Url + "services/" + serviceName + "/plugins?size=" + size);
        } else {
            response = requestcommon.GetKong(Url + "services/" + serviceName + "/plugins?size=100");
        }
        return response;
    }

    /**
     * 获取所有plugin
     *
     * @param size
     * @return
     */
    public String ListAllPlugin(int size) {
        if (size > 0) {
            response = requestcommon.GetKong(Url + "plugins?size=" + size);
        } else {
            response = requestcommon.GetKong(Url + "plugins?size=100");
        }
        return response;
    }


    /**
     * 删除plugin
     *
     * @param serviceName
     * @param pluginName
     * @return
     */
    public String DeletePluginInService(String serviceName, String pluginName) {
        response = requestcommon.DeleteKong(Url + "services/" + serviceName + "/plugins/" + pluginName);
        return response;
    }

    /**
     * 更新plugin
     *
     * @param serviceName
     * @param pluginName
     * @param requestBody
     * @return
     */
    public String PutPluginInService(String serviceName, String pluginName, JSONObject requestBody) {
        response = requestcommon.PutKong(Url + "services/" + serviceName + "/plugins/" + pluginName, requestBody);
        return response;
    }

    /**
     * 更新plugin
     *
     * @param serviceName
     * @param pluginName
     * @param requestBody
     * @return
     */
    public String PatchPluginInService(String serviceName, String pluginName, JSONObject requestBody) {
        response = requestcommon.PatchKong(Url + "services/" + serviceName + "/plugins/" + pluginName, requestBody);
        return response;
    }

    /**
     * 获取plugin
     *
     * @param serviceName
     * @param pluginName
     * @return
     */
    public String FetachPluginInService(String serviceName, String pluginName) {
        response = requestcommon.GetKong(Url + "consumers/" + serviceName + "/plugins/" + pluginName);
        return response;
    }

    /**
     * 创建consumer
     *
     * @param requestBody
     * @return
     */
    public String CreateConsumer(JSONObject requestBody) {
        response = requestcommon.PostKong(Url + "consumers", requestBody);
        return response;
    }

    /**
     * 获取所有consumer
     *
     * @param size
     * @return
     */
    public String ListAllConsumer(int size) {
        if (size > 0) {
            response = requestcommon.GetKong(Url + "consumers?size=" + size);
        } else {
            response = requestcommon.GetKong(Url + "consumers?size=100");
        }
        return response;
    }

    /**
     * 删除consumer
     *
     * @param commerceName
     * @return
     */

    public String DeleteConsumer(String commerceName) {
        response = requestcommon.DeleteKong(Url + "consumers/" + commerceName);
        return response;
    }

    /**
     * 更新consumer
     *
     * @param commerceName
     * @param requestBody
     * @return
     */
    public String PutConsumer(String commerceName, JSONObject requestBody) {
        response = requestcommon.PutKong(Url + "consumers/" + commerceName, requestBody);
        return response;
    }

    /**
     * 更新consumer
     *
     * @param commerceName
     * @param requestBody
     * @return
     */
    public String PatchConsumer(String commerceName, JSONObject requestBody) {
        response = requestcommon.PatchKong(Url + "consumers/" + commerceName, requestBody);
        return response;
    }
    public String PatchPlugin(String pluginsID, JSONObject requestBody) {
        response = requestcommon.PatchKong(Url + "plugins/" + pluginsID, requestBody);
        return response;
    }
    /**
     * 获取consumer
     *
     * @param commerceName
     * @return
     */
    public String FetchConsumer(String commerceName) {
        response = requestcommon.GetKong(Url + "consumers/" + commerceName);
        return response;
    }
}
